import Cookies from "js-cookie";
import { getLanguage } from "@/lang/index";
import { fetchCodeList, fetchUserList, fetchDutyList, updatePassword } from "@/api/code/index";
const state = {
    sidebar: {
        opened: true, //Cookies.get('sidebarStatus') ? !!+Cookies.get('sidebarStatus') : true, 是否折叠菜单
        withoutAnimation: false
    },
    device: "desktop",
    language: getLanguage(),
    size: Cookies.get("size") || "medium",
    allCode: {},
    allDuty: [],
    allUser: []
};
//转树形结构
const optionData = function(e, b) {
    if (e) {
        let cloneData = JSON.parse(JSON.stringify(e)); // 对源数据深度克隆
        //这里加一个逻辑，在选择范围字段编辑和新增的下拉中直接显示，在表格中更需要过滤对比存在的数据高亮,e为元数据,b为对比数据
        // if(b){
        //   
        //  //b存在则对比较数据循环对比
        //   a.forEach(e=>{
        //     if(b.indexOf(e.id)){
        //        e.highlight=true
        //     }
        //   })

        // }
        return cloneData.filter(father => {
            // 循环所有项，并添加children属性
            let branchArr = cloneData
                .filter(child => father.id == child.parentId)
                .sort((a, b) => a.typeOrder - b.typeOrder); // 返回每一项的子级数组

            father.name = father.organName || father.typeName; //兼容部门和设备分类
            father.label = father.organName || father.typeName;
            father.value = father.organId || father.typeId;
            branchArr.name = branchArr.organName || branchArr.typeName;
            branchArr.label = branchArr.organName || branchArr.typeName;
            branchArr.value = branchArr.organId || branchArr.typeId;

            branchArr.length > 0 ? (father.children = branchArr) : ""; //给父级添加一个children属性，并赋值
            return father.parentId == 0; //返回第一层
        });
    }
};
const mutations = {
    TOGGLE_SIDEBAR: state => {
        state.sidebar.opened = !state.sidebar.opened;
        state.sidebar.withoutAnimation = false;
        if (state.sidebar.opened) {
            Cookies.set("sidebarStatus", 1);
        } else {
            Cookies.set("sidebarStatus", 0);
        }
    },
    CLOSE_SIDEBAR: (state, withoutAnimation) => {
        Cookies.set("sidebarStatus", 0);
        state.sidebar.opened = false;
        state.sidebar.withoutAnimation = withoutAnimation;
    },
    TOGGLE_DEVICE: (state, device) => {
        state.device = device;
    },
    SET_LANGUAGE: (state, language) => {
        state.language = language;
        Cookies.set("language", language);
    },
    SET_SIZE: (state, size) => {
        state.size = size;
        Cookies.set("size", size);
    },
    UPDATE_ALLCODE: (state, code) => {
        //code应该为一个json对象，key为code的编码

        state.allCode[code.name] = code.codeList;
    },
    SET_ALLUSER: (state, args) => {
        //code应该为一个json对象，key为code的编码

        state.allUser = args;
    },
    SET_ALLDUTY: (state, args) => {
        //code应该为一个json对象，key为code的编码

        state.allDuty = args;
    }
};

const actions = {
    toggleSideBar({ commit }) {
        commit("TOGGLE_SIDEBAR");
    },
    closeSideBar({ commit }, { withoutAnimation }) {
        commit("CLOSE_SIDEBAR", withoutAnimation);
    },
    toggleDevice({ commit }, device) {
        commit("TOGGLE_DEVICE", device);
    },
    setLanguage({ commit }, language) {
        commit("SET_LANGUAGE", language);
    },
    setSize({ commit }, size) {
        commit("SET_SIZE", size);
    },
    updatePwd({ commit, state }, args) {
        return updatePassword(args)
    },
    async getCodeList({ commit, state }, codeArray) {

        return await fetchCodeList(codeArray).then(response => {
            console.log("getCodeList", response);

            for (let key in response) {
                console.log(response[key], "+++response[key]++=");
                if (key == "department" || key == "assetType") {
                    commit("UPDATE_ALLCODE", {
                        codeList: optionData(response[key]),
                        name: key
                    });
                } else {
                    commit("UPDATE_ALLCODE", { codeList: response[key], name: key });
                }
            }

        });

    },
    updateAllUser({ commit, state }, codeArray) {
        //请求用户列表
        console.log(codeArray, "+++codeArray+++");
        return fetchUserList(codeArray).then(response => {
            console.log("+++allUser++", response);
            const data = response.data.map(e => {
                return { label: e.realName, value: e.userId };
            });
            commit("SET_ALLUSER", data);
        });
    },
    //请求职位列表
    updateAllDuty({ commit, state }, codeArray) {
        return fetchDutyList(codeArray).then(response => {
            const data = response.data.map(e => {
                return { label: e.positionsName, value: e.positionsId };
            });
            commit("SET_ALLDUTY", data);
        });
    }
};

export default {
    namespaced: true,
    state,
    mutations,
    actions
};